<?php

namespace App\Kuafu\Banner\Resources;

use App\Kuafu\Banner\Enum\BannerEnum;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\CouponPackage\Enum\CouponPackageEnum;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;

class BannerListResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            'id'            => $this->id,
            'img'           => $this->img ? CommonService::joinImageUrl($this->img) : "",
            'extraImg'      => $this->extraImg ? CommonService::joinImageUrl($this->extraImg) : '',
            'brandId'       => $this->brandId,
            'name'          => $this->name,
            'type'          => $this->type,
            'typeText'      => BannerEnum::TYPE_MAP[$this->type],
            'shopType'      => $this->shopType,
            'effectiveType' => $this->effectiveType,
            'startAt'       => $this->startAt > 0 ? date('Y-m-d', $this->startAt) : null,
            'endAt'         => $this->endAt > 0 ? date('Y-m-d', $this->endAt) : null,
            'sort'          => $this->sort,
            'shop'          => $this->getShopText($this->shopType, $this->bannerShopMappingRelate),
            'shopLists'     => $this->getShopLists(),
            'status'        => $this->status,
            'bannerType'    => $this->bannerType,
            'isDeleted'     => $this->isDeleted,
            'imgs'          => $this->imgs ? json_decode($this->imgs, true) : [],
            'productId'     => $this->productId,
            'bannerJoinId'  => $this->bannerJoinId,
        ];
    }

    /**
     * @param int $shopType
     * @param Collection|null $bannerShopMappingRelate
     * @return string
     */
    public function getShopText(int $shopType, Collection $bannerShopMappingRelate = null): string
    {
        if ($shopType == CouponPackageEnum::SHOP_TYPE_SELECTION) {
            return vsprintf('%d家门店', [$bannerShopMappingRelate->count()]);
        }
        return CouponPackageEnum::SHOP_TYPES[$shopType];
    }

    /**
     * 获取门店信息
     *
     * @return array
     */
    public function getShopLists(): array
    {
        $shopLists = [];
        if ($this->whenLoaded('bannerShopMappingRelate') && $this->bannerShopMappingRelate->isNotEmpty()) {
            $this->bannerShopMappingRelate->each(function ($v, $k) use (&$shopLists) {
                array_push($shopLists, [
                    'id'       => $v->mappingShopRelate ? $v->mappingShopRelate->id : null,
                    'name'     => $v->mappingShopRelate ? $v->mappingShopRelate->name : null,
                    'shopCode' => $v->mappingShopRelate ? CommonService::zeroFill($v->mappingShopRelate['shopCode']) : null
                ]);
            });
        }
        return $shopLists;
    }
}
